package com.qf.service.impl;

import com.qf.dao.AddressDao;
import com.qf.dao.OrderDao;
import com.qf.dao.impl.AddressDaoImpl;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.domain.Address;
import com.qf.service.AddressService;
import com.qf.util.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/20
 */
public class AddressServiceImpl implements AddressService {

    AddressDao addressDao = new AddressDaoImpl();
    @Override
    public List<Address> findAddListByUid(int id) {
        return addressDao.findAddListByUid( id);
    }

    @Override
    public void insert(Address address) {
        addressDao.insert(address);
    }

    OrderDao orderDao = new OrderDaoImpl();
    @Override
    public void deleteById(String id) {
        long count = orderDao.findCountByAid(id);
        if(count==0){
            addressDao.deleteById(id);
        }else{
            System.out.println("与订单关联不能删除");
        }
        addressDao.deleteById(id);
    }

    @Override
    public void update(Address address) {
        addressDao.update(address);
    }

    @Override
    public void setDefaultAddress(String aid, int uid) {

        try {
            DataSourceUtils.begin();
            addressDao.clearDefaultAddress(uid);

            addressDao.setDefaultAddress(aid);
            DataSourceUtils.commit();
        } catch (SQLException e) {
            try {
                DataSourceUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }

        }finally {
            try {
                DataSourceUtils.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
